﻿using System;
using ElectronicCommerce.Framework.Web.ResourceManagement;


namespace ElectronicCommerce.Framework.Web.Mvc.WebForm
{
    public abstract class CustomAjaxPageBase<TCustomContext> : System.Web.UI.Page
        where TCustomContext : CustomContextBase
    {
        public CustomAjaxPageBase()
        {
            CustomContext = Activator.CreateInstance<TCustomContext>();
            Url = new UrlBuilder();
            Resource = new ResourceBuilder();
        }

        #region 基础页属性
        internal Enum InternalViewPageAlias { get; set; }

        /// <summary>
        /// Http Content Type
        /// </summary>
        public virtual string ContentType { get { return "text/html"; } }

        /// <summary>
        /// 页面上下文
        /// </summary>
        public TCustomContext CustomContext { get; private set; }

        /// <summary>
        /// 页面链接生成器
        /// </summary>
        public UrlBuilder Url { get; private set; }

        /// <summary>
        /// 资源文件生成器
        /// </summary>
        public ResourceBuilder Resource { get; private set; }

        /// <summary>
        /// 显示页面数据集
        /// </summary>
        public virtual CustomModelContainer<TCustomContext> Model { get; protected set; }

        /// <summary>
        /// 页面控制器
        /// </summary>
        public CustomControllerBase Controller { get; set; }
        #endregion

        #region 页面事件流程
        protected override void OnPreInit(EventArgs e)
        {
            //Render SERVER ID
            CustomContext.Response.AddHeader("X-Who", System.Net.Dns.GetHostName());

            Model = new CustomModelContainer<TCustomContext>(CustomContext, Controller.Models);

            Response.ContentType = ContentType;

            base.OnPreInit(e);
        }

        protected override void OnPreRender(EventArgs e)
        {

            base.OnPreRender(e);
        }
        #endregion
    }

    public abstract class CustomAjaxPageBase<
        TController,
        TModel,
        TCustomContext,
        TPageAliasType> : CustomAjaxPageBase<TCustomContext>
        where TController : CustomControllerBase
        where TModel : CustomModelBase
        where TCustomContext : CustomContextBase
        where TPageAliasType : struct
    {
        private CustomModelContainer<TCustomContext, TModel> _model = null;

        public abstract TPageAliasType ViewPageAlias { get; }

        new public CustomModelContainer<TCustomContext, TModel> Model
        {
            get
            {
                if (_model == null)
                {
                    _model = new CustomModelContainer<TCustomContext, TModel>(CustomContext, base.Model.ModelDictionary);
                }

                return _model;
            }
        }

        protected override void OnPreInit(EventArgs e)
        {
            base.InternalViewPageAlias = ViewPageAlias as Enum;

            TController controller = Activator.CreateInstance<TController>();
            controller.CustomContext = CustomContext;
            controller.ViewPage = this;
            Controller = controller;
            base.OnPreInit(e);
        }

        new public TController Controller
        {
            get { return base.Controller as TController; }
            set { base.Controller = value; }
        }
    }
}
